﻿using System;
using System.Collections.Generic;
using Seeker.Business.Interface;
using System.Text.RegularExpressions;
using System.Web;
using WeeLibrary;
using Seeker.DAL.Admin;
using Seeker.Model.Interface;
using System.Text;

namespace Seeker.Business.Admin
{
    public class AdminUsers: IAdminUsersLoginVerify
    {
        /// <summary>
        /// 验证登陆
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public string LoginVerify(string userName, string password)
        {
            //验证账户，须由至少5位的字符，数字，下划线组成
            Regex verifyChar = new Regex(@"^[\w\d_]{5,30}$");
            password = WeeLibrary.Text.HashEncrypt.GetPassword(password);

            if (verifyChar.IsMatch(userName))
            {
                IBaseAUserInfo userInfo = new Login().SignIn(userName, password);

                if (userInfo != null)
                {
                    HttpContext context = HttpContext.Current;
                    context.Session["UID"] = userInfo.UID;
                    context.Session["Account"] = userInfo.Account;
                    context.Session["Key"] = userInfo.Key;
                }
                else
                {
                    return "用户名或密码错误";
                }

                return "";

            }
            else
            {
                return "用户名含有非法字符且应为5-30个字符";
            }
        }


        /// <summary>
        /// 通过uid和key判断用户的有效性
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool LoginVerify(int uid, string key)
        {
            if (key == null) return false;
            return new Login().LoginVerify(uid, key);
        }


        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public IBaseAUserFullInfo GetUserInfo(int uid)
        {

            if (HttpContext.Current.Session["UID"] != null)
            {
                return new DAL.Admin.AUserInfo().GetInfo(uid);
            }
            else
            {
                return null;
            }

        }


        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="current">当前页</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="userName">用户名，模糊查询</param>
        /// <param name="status">状态，可以为null</param>
        /// <param name="orderBy">排序</param>
        /// <returns></returns>
        public List<Model.AUsers> GetUserList(int current, int pageSize, string userName, bool? status, string orderBy)
        {
            if (HttpContext.Current.Session["UID"] == null)
            {
                return null;
            }

            StringBuilder where = new StringBuilder();

            if (userName != null || userName != "")
            {
                where.Append(" (");
                where.Append(new WeeLibrary.Text.SQLText().ConvertLikeText("ACCOUNT", userName));

                //若为数字，则也查询UID是否存在
                if (new Regex(@"\d").IsMatch(userName))
                {
                    where.Append("OR UID = " + userName);
                }

                where.Append(")");
            }

            if (status != null)
            {

                if (where.Length > 0)
                {
                    where.Append(" AND STATUS = " + Convert.ToByte(status));
                }
                else
                {
                    where.Append(" STATUS = " + Convert.ToByte(status));
                }

            }

            orderBy = orderBy.ToLower() == "a" ? "UID ASC" : "UID DESC";

            return new DAL.Admin.AUserInfo().GetInfoList(current, pageSize, where.ToString(), orderBy);

        }
    }
}
